package com.corffen.design.strategy;

public class Animator {
    private Interpolator mInterpolator = new LinearInterpolator();
    private long mDuration = 600;
    private final String mProperty = "property";
    private long interval = 100;
    private float mStartValue = 0.0f;
    private float mEndValue = 200.0f;
    private float mCurrentValue;

    private UpdateAnimationListener mUpdateAnimationListener;

    public void setInterpolator(Interpolator interpolator) {
        mInterpolator = interpolator;
    }

    public void setDuration(long duration) {
        mDuration = duration;
    }

    public void setValue(float... values) {
        if (values.length == 1) {
            mEndValue = values[0];
        } else if (values.length == 2) {
            mStartValue = values[0];
            mEndValue = values[1];
        } else {
            System.out.println("请设置合适的数据..");
        }

    }

    public void start() {
        long start = System.currentTimeMillis();
        long end = start + mDuration;
        for (long i = start; i <= end; i += interval) {
            float usedTime = (i - start) * 1.0f / mDuration;
            float currentValue = mInterpolator.getInterpolation(usedTime);
            mCurrentValue = mStartValue + currentValue * (mEndValue - mStartValue);
            if (mUpdateAnimationListener != null) {
                mUpdateAnimationListener.updateAnimator(mCurrentValue);
            }
        }
    }

    public interface UpdateAnimationListener {
        void updateAnimator(float value);
    }

    public void addUpdateAnimatorListener(UpdateAnimationListener listener) {
        mUpdateAnimationListener = listener;
    }

    public String getProperty() {
        return mProperty;
    }

    public float getCurrentValue() {
        return mCurrentValue;
    }
}
